package com.zhaocaimao.framework.common.response;

import com.zhaocaimao.framework.common.exception.BaseExceptionInterface;
import com.zhaocaimao.framework.common.exception.BizException;
import lombok.Data;

import java.io.Serializable;

//@Data注解是lombok提供的，作用是自动生成getter和setter方法
@Data
//Serializable接口是java序列化接口，作用是允许对象被序列化，以便在网络传输中传输对象。
public class Response<T> implements Serializable {

    //是否成功,默认为true
    private boolean success = true;

    //响应消息
    private String message;

    //异常码
    private String errorCode;

    //响应数据
    private T data;

    //====================================成功响应=======================================
    public static <T> Response<T> success(){
        Response<T> response = new Response<>();
        return response;
    }

    public static <T> Response<T> success(T data){
        Response<T> response = new Response<>();
        response.setData(data);
        return response;
    }

    //====================================失败响应=======================================
    public static <T> Response<T> fail(){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        return response;
    }
    public static <T> Response<T> fail(String errorMessage){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(errorMessage);
        return response;
    }
    public static <T> Response<T> fail(String errorCode, String errorMessage){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(errorCode);
        response.setMessage(errorMessage);
        return response;
    }
    public static <T> Response<T> fail(BizException bizException){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(bizException.getErrorCode());
        response.setMessage(bizException.getErrorMessage());
        return response;
    }
    public static <T> Response<T> fail(BaseExceptionInterface baseExceptionInterface){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(baseExceptionInterface.getErrorCode());
        response.setMessage(baseExceptionInterface.getErrorMessage());
        return response;
    }

}
